package com.sikao.app.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.adapter.GvAddStepPricesTypeProfilePicItemAdapter;
import com.sikao.app.bean.Bean;
import com.sikao.app.bean.SubmitAcceptanceReportBean;
import com.sikao.app.bean.UploadPicturesBean;
import com.sikao.app.dao.SubmitAcceptanceReportModel;
import com.sikao.app.dao.UploadPicturesModel;
import com.sikao.app.myinterface.Callback;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.EditTextUtils;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.view.MyGridView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 提交验收报告
 */
public class SubmitAcceptanceReportActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;

	private TextView tvWeightUnit;
	private EditText tvReceivedWeight;
	private TextView tvUnit;
	private EditText tvFreight;
	private EditText tvFinalPrice;
	private MyGridView gridView;
	private Button btn;

	private SubmitAcceptanceReportModel submitAcceptanceReportModel;
	private SubmitAcceptanceReportBean submitAcceptanceReportBean;
	private UploadPicturesModel uploadPicturesModel;

	private List<String> picList = new ArrayList<>();
	private List<String> uploadPicList = new ArrayList<>();
	private List<String> picIdList = new ArrayList<>();

	private int picIndex = 0;//点击的图片的下标
	private GvAddStepPricesTypeProfilePicItemAdapter adapter;
	public static final int REQUEST_GET_PHOTO = 1001;
	private AlertDialog dialog;

	private String unit;
	private String beanId;
	private DecimalFormat format1 =new DecimalFormat("0.000");
	private DecimalFormat format2 =new DecimalFormat("0.00");

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_submit_acceptance_report);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {
		picList.add("");
		unit = getIntent().getStringExtra("unit");
		beanId = getIntent().getStringExtra("bean");
	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		tvWeightUnit = (TextView) findViewById(R.id.tv_weight_unit);
		tvReceivedWeight = (EditText) findViewById(R.id.tv_received_weight);
		tvUnit = (TextView) findViewById(R.id.tv_unit);
		tvFreight = (EditText) findViewById(R.id.tv_freight);
		tvFinalPrice = (EditText) findViewById(R.id.tv_final_price);
		gridView = (MyGridView) findViewById(R.id.gridView);
		btn = (Button) findViewById(R.id.btn);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		btn.setOnClickListener(this);
	}

	@Override
	protected void init() {
		titleTv.setText("验收报告");
		backIv.setVisibility(View.VISIBLE);
		EditTextUtils.setPricePoint(3,tvReceivedWeight);
		EditTextUtils.setPricePoint(2,tvFreight);
		EditTextUtils.setPricePoint(2,tvFinalPrice);

		if (unit != null) {
			tvWeightUnit.setText(unit);
			tvUnit.setText(unit);
		}
		adapter = new GvAddStepPricesTypeProfilePicItemAdapter(this, picList, new Callback() {
			@Override
			public void notice(int position) {
				picIndex = position;
				if (position == (picList.size() - 1)) {//第一次点击或者点的是最后一个
					Intent intent = new Intent(SubmitAcceptanceReportActivity.this, SelectPictureActivity.class);
					startActivityForResult(intent, REQUEST_GET_PHOTO);
					overridePendingTransition(R.anim.bottom_push_in, R.anim.bottom_push_out);
				} else {
					createDialog();
				}
			}
		});
		gridView.setAdapter(adapter);
	}


	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
				case REQUEST_GET_PHOTO:
					String picturePath = data.getStringExtra("path");
					picList.set(picIndex, picturePath);
					uploadPicture(picturePath);
					//添加一个新的空的照片
					picList.add("");
					adapter.notifyDataSetChanged();
					break;
				default:
					break;
			}
		}
	}

	private void createDialog() {
		if (dialog == null) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("确认删除吗?");
			builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					picList.remove(picIndex);
					uploadPicList.remove(picIndex);
					picIdList.remove(picIndex);
					adapter.notifyDataSetChanged();
				}
			});
			builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			dialog = builder.create();
		}
		dialog.show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.btn:
				submitAcceptanceReport();
				break;
			default:
				break;
		}
	}

	/**
	 * 提交验收报告
	 */
	private void submitAcceptanceReport() {
		if (tvReceivedWeight.getText().toString().length() == 0) {
			Toast.makeText(this, "请填写收货重量", Toast.LENGTH_SHORT).show();
			return;
		}
		if (tvFreight.getText().toString().length() == 0) {
			Toast.makeText(this, "请填写运费价格", Toast.LENGTH_SHORT).show();
			return;
		}
		if (tvFinalPrice.getText().toString().length() == 0) {
			Toast.makeText(this, "请填写最终成交价", Toast.LENGTH_SHORT).show();
			return;
		}
		if (submitAcceptanceReportModel == null) {
			submitAcceptanceReportBean = new SubmitAcceptanceReportBean();
			submitAcceptanceReportModel = new SubmitAcceptanceReportModel(this, new OnNetBackListener<Bean>() {
				@Override
				public void netBack(Bean data) {
					if (data == null) {
						return;
					}
					if (data.getStatus().equals("1")) {
						Toast.makeText(SubmitAcceptanceReportActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
						Intent intent = getIntent();
						intent.putExtra("", "");
						setResult(RESULT_OK, intent);
						finish();
					} else {
						Toast.makeText(SubmitAcceptanceReportActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
		submitAcceptanceReportBean.setAction("postReport");
		submitAcceptanceReportBean.setOrderId(beanId);
		submitAcceptanceReportBean.setFinalNumber(format1.format(Double.valueOf(tvReceivedWeight.getText().toString())));
		submitAcceptanceReportBean.setFreight(format2.format(Double.valueOf(tvFreight.getText().toString())));
		submitAcceptanceReportBean.setFinalPrice(format2.format(Double.valueOf(tvFinalPrice.getText().toString())));
		String fileIds = "";
		String filePaths = "";
		for (int i = 0; i < uploadPicList.size(); i++) {
			fileIds = fileIds + picIdList.get(i);
			filePaths = filePaths + uploadPicList.get(i);
			if (i != uploadPicList.size() - 1) {
				fileIds = fileIds + ",";
				filePaths = filePaths + ",";
			}
		}
		submitAcceptanceReportBean.setFileIds(fileIds);
		submitAcceptanceReportBean.setFilePaths(filePaths);
		submitAcceptanceReportModel.request(JSONUtil.toJSON(submitAcceptanceReportBean), true);
	}

	/**
	 * 上传图片
	 */
	private void uploadPicture(String picturePath) {
		if (uploadPicturesModel == null) {
			uploadPicturesModel = new UploadPicturesModel(this, new OnNetBackListener<UploadPicturesBean>() {
				@Override
				public void netBack(UploadPicturesBean data) {
					if (data == null) {
						Toast.makeText(SubmitAcceptanceReportActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
						return;
					}

					if (data.getStatus().equals("1")) {
						uploadPicList.add(data.getRes().getArr().get(0).getUrl());
						picIdList.add(data.getRes().getArr().get(0).getId());
					} else {
						Toast.makeText(SubmitAcceptanceReportActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
		uploadPicturesModel.request(picturePath, true);
	}
}
